//
//  YXForgetPasswordViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXForgetPasswordViewController.h"

@interface YXForgetPasswordViewController ()

@end

@implementation YXForgetPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"重置密码";
    canSend = YES;
    UIBarButtonItem * backBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(touchBackBtn)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    YXForgetPasswordView * forgetPasswordView = [[YXForgetPasswordView alloc] init];
    self.view = forgetPasswordView;
    _forgetPasswordView = forgetPasswordView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    [_forgetPasswordView.authcodeText addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
    [_forgetPasswordView.getAuthcodeBtn addTarget:self action:@selector(touchGetAuthcodeBtn) forControlEvents:UIControlEventTouchUpInside];
    [_forgetPasswordView.inputPasswordText addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
    [_forgetPasswordView.affirmPasswordText addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];
    [_forgetPasswordView.resetPasswordBtn addTarget:self action:@selector(touchResetPasswordBtn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - ButtonsClick
- (void)touchBackBtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchGetAuthcodeBtn
{
    [self.view endEditing:YES];
    if (_forgetPasswordView.numberText.text.length != 11) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入正确手机号码。"];
        return;
    }
    if (canSend) {
        canSend = NO;
        [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"resetCode",@"type",_forgetPasswordView.numberText.text,@"phone", nil] dataBlock:^(NSDictionary *dic) {
            NSString * resultCode = [dic objectForKey:@"resultCode"];
            if ([resultCode isEqualToString:@"0"]) {
                [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"发送验证码成功，请查看你的手机。"];
            }else{
                [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
            }
        }];
    }
}

- (void)touchResetPasswordBtn
{
    [self.view endEditing:YES];
    if (_forgetPasswordView.numberText.text.length != 11) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入正确的手机号。"];
        return;
    }
    if ([_forgetPasswordView.authcodeText.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入验证码。"];
        return;
    }
    if ([_forgetPasswordView.inputPasswordText.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入密码。"];
        return;
    }
    if (![_forgetPasswordView.inputPasswordText.text isEqualToString:_forgetPasswordView.affirmPasswordText.text]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"两次输入密码不一致。"];
        return;
    }
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"resetPassWord",@"type",_forgetPasswordView.numberText.text,@"phone",_forgetPasswordView.inputPasswordText.text,@"passWord",_forgetPasswordView.authcodeText.text,@"code", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"修改用户密码成功。"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

#pragma mark - KeyboardNotification
- (void)keyboardDown
{
    [self.view endEditing:YES];
}

- (void)keyboardShow:(NSNotification *)nofi
{
    CGRect scrollRect = _forgetPasswordView.backgroundScrollVi.frame;
    CGRect keyboardRect = [[nofi.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    [UIView animateWithDuration:[[nofi.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        _forgetPasswordView.backgroundScrollVi.frame = CGRectMake(scrollRect.origin.x, scrollRect.origin.y, scrollRect.size.width, scrollRect.size.height - keyboardRect.size.height);
    }];
}

- (void)keyboardHidden:(NSNotification *)nofi
{
    CGRect scrollRect = _forgetPasswordView.backgroundScrollVi.frame;
    CGRect keyboardRect = [[nofi.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    _forgetPasswordView.backgroundScrollVi.frame = CGRectMake(scrollRect.origin.x, scrollRect.origin.y, scrollRect.size.width, scrollRect.size.height + keyboardRect.size.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
